package com.example.administrator.testxingpiao.httputils;

import android.util.Log;

import com.example.administrator.testxingpiao.bean.ExerciseBean;
import com.example.administrator.testxingpiao.bean.GoodsListBean;
import com.example.administrator.testxingpiao.bean.HomePageBean;
import com.example.administrator.testxingpiao.bean.ListBean;
import com.example.administrator.testxingpiao.bean.ListExerciseBean;
import com.example.administrator.testxingpiao.bean.PerformBean;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/8/4.
 */
public class HttpUtils {
    public static final int HOME_PAGE = 0;

    private static HttpUtils instance;

    public static synchronized HttpUtils getInstance(){
        if(instance == null){
            instance = new HttpUtils();
        }
        return instance;
    }

    public interface HttpCallBack<T>{
        public void onSuceess(T t);

        public void onFail();
    }

    public<T> void getHomePageData(HashMap<String,String> params,final HttpCallBack<T> beanHttpCallBack){

        Call<HomePageBean> homePageBeanCall = RetrofitHelper
                .getInstance().createRetrofitService(DataService.class)
                .getHomePage(params);

        homePageBeanCall.enqueue(new Callback<HomePageBean>() {
            @Override
            public void onResponse(Call<HomePageBean> call, Response<HomePageBean> response) {
                List<ListBean> list = response.body().getResult().getRecommendInfo2().getList();
                Log.i("TAG","code:" + response.body().getCode());

                beanHttpCallBack.onSuceess((T) response.body());
            }

            @Override
            public void onFailure(Call<HomePageBean> call, Throwable t) {
                beanHttpCallBack.onFail();
            }
        });
    }

    public void getExercise(HashMap<String,String> params,final HttpCallBack<ExerciseBean> beanHttpCallBack){
        Call<ExerciseBean> exerciseBeanCall=RetrofitHelper.getInstance()
                .createRetrofitService(ExerciseService.class)
                .getExercise(params);
        exerciseBeanCall.enqueue(new Callback<ExerciseBean>() {
            @Override
            public void onResponse(Call<ExerciseBean> call, Response<ExerciseBean> response) {
                List<ListExerciseBean> list = response.body().getResult().getList();
                for(ListExerciseBean lb : list){
                    Log.i("TAG","图片" +lb.getUrlWap() );
                }
                beanHttpCallBack.onSuceess(response.body());
            }

            @Override
            public void onFailure(Call<ExerciseBean> call, Throwable t) {
                beanHttpCallBack.onFail();
            }
        });
    }


    public static byte[] getData(String path) {
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        int len = 0;
        byte[] b = new byte[1024];
        try {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            if (conn.getResponseCode() == 200) {
                is = conn.getInputStream();
                baos = new ByteArrayOutputStream();

                while ((len = is.read(b)) != -1) {
                    baos.write(b, 0, len);
                    baos.flush();
                }
                byte[] data = baos.toByteArray();
                return data;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
                baos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    public void getPerformData(HashMap<String,String> params, final HttpCallBack<PerformBean> beanHttpCallBack){
        log("进入getFr");
        final Call<PerformBean> performPageBeanCall =RetrofitHelper
                .getInstance()
                .createRetrofitService(ThreePageService.class)
                .getPerformData(params);
        performPageBeanCall.enqueue(new Callback<PerformBean>() {
            @Override
            public void onResponse(Call<PerformBean> call, Response<PerformBean> response) {
                log("进入方法");
                List<GoodsListBean> goodsList = response.body().getDatas().getGoodsList();
                for (GoodsListBean gb : goodsList) {
                    log("code:"+response.code());
                    log("cod2:"+gb.getGoodsName());
                }
                beanHttpCallBack.onSuceess(response.body());
            }
            @Override
            public void onFailure(Call<PerformBean> call, Throwable t) {
                log("加载失败！");
                beanHttpCallBack.onFail();
            }
        });
    }
    public void log(String msg){
        Log.i("TAG",msg);
    }
}
